<?php
/**
 *  产品类别的curd操作
 * Author: lyun
 * Date: 2018/1/25
 * Created by Panxsoft.
 */

namespace App\Http\Controllers\Admin;

use App\Exceptions\AdminManageException;
use App\Http\Models\Admin\ProductCategory;
use App\Http\Requests\Admin\ProductCategoriesIndexRequest;
use App\Http\Requests\Admin\ProductCategoriesStoreRequest;
use App\Http\Requests\Admin\ProductCategoriesUpdateRequest;
use App\Services\Utils\ApiChecker;
use App\Services\Utils\ApiResponser;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ProductCategoriesController extends Controller
{
    use ApiChecker,ApiResponser;

    /**
     * 返回所有的产品类别信息
     *
     * @param ProductCategoriesIndexRequest $request
     * @return \Illuminate\Http\Response
     */
    public function index(ProductCategoriesIndexRequest $request)
    {
        $product_categories = ProductCategory::where('super_id', ProductCategory::TOP_CATEGORY)
            ->with('getSubCategories')
            ->latest('priority')
            ->paginate($request->per_page?:15);

        return $this->response(compact('product_categories'));
    }

    /**
     *  返回一级种类的信息
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // 返回一级种类的数组
        $super_categories = ProductCategory::getSupCategories();

        return $this->response(compact('super_categories'));
    }

    /**
     * 添加产品的种类
     *
     * @param  ProductCategoriesStoreRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ProductCategoriesStoreRequest $request)
    {
        $product_categories = $this->transformProductCategoriesData($request);

        $stored = ProductCategory::create($product_categories);

        return $this->response(compact('stored'));
    }

    /**
     * @param ProductCategoriesUpdateRequest $request
     * @param $id
     * @return array|\Illuminate\Http\Response
     */
    public function update(ProductCategoriesUpdateRequest $request, $id)
    {
        $product_category = ProductCategory::find($id);
            $this->checkItemExist($product_category, '产品类别');

        $product_category_data = $this->transformProductCategoriesData($request);

        $updated = $this->updateProductCategory($product_category_data, $product_category);

        return $this->response(compact('updated'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $product_category = ProductCategory::find($id);
            $this->checkItemExist($product_category, '产品类别');

        $deleted = $this->deleteProductCategory($product_category);

        return $this->response(compact('deleted'));
    }

    /**
     * @param ProductCategory $product_category
     * @return bool|null
     */
    private function deleteProductCategory(ProductCategory $product_category)
    {
        if (($product_category->super_id === ProductCategory::TOP_CATEGORY) &&
            ($product_category->checkHasSubCategories()))
        {
            $this->checkCustomValue(false, '一级种类下还有二级种类，不能删除！');
        } else if ($product_category->checkHasProduction()) {
            $this->checkCustomValue(false, '二级种类下面有商品，不能删除！');
        }

        $deleted = $product_category->delete();

        return $deleted;
    }

    /**
     * @param array $product_category_data
     * @param ProductCategory $product_category
     * @throws AdminManageException
     * @return bool
     */
    private function updateProductCategory(array $product_category_data, ProductCategory $product_category)
    {
        $super_id = $product_category->super_id;

        if (($super_id === ProductCategory::TOP_CATEGORY) && ((int)$product_category_data['super_id'] !== $super_id)) {
            $this->checkCustomValue(false, '一级种类的上级只能是顶级类别');
        } else if (($super_id !== ProductCategory::TOP_CATEGORY) &&
            (int)$product_category_data['super_id'] === ProductCategory::TOP_CATEGORY
        ) {
            $this->checkCustomValue(false, '二级种类的上级只能是一级种类');
        }

        $updated = $product_category->update($product_category_data);

        return $updated;
    }

    /**
     * @param ProductCategoriesStoreRequest|ProductCategoriesUpdateRequest|Request $request
     * @return array
     */
    private function transformProductCategoriesData(Request $request)
    {
        return [
            'super_id'    => $request->super_id,
            'name'        => $request->name,
            'description' => $request->description,
            'priority'    => $request->priority?:0,
            'thumb'       => $request->thumb,
        ];
    }
}
